//http://www.nirsoft.net/utils/smsniff.html
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Pricom
{
    public class Webchat
    {
        public Webchat()
        {
            Test();
            //System.Diagnostics.Debug.WriteLine(incomingText);
        }
        public void Test()
        {
            string channel = "coderfun";
            string message = "Hello%20World,%20from%20CSharp!";
            HttpPost("http://webchat.xs4all.nl/cgi-bin/ircnet/irc.cgi", "Server=IRCnet&interface=mozilla&Nickname=[sl]Ivan2&channel_select=other&Channel=%23" + channel + "&login.x=196&login.y=316&login=login&Realname=CGI%3AIRC+User&Format=default&Character_set=");
            string random = GetRandomValue();

            //HttpGet("http://webchat.xs4all.nl/cgi-bin/ircnet/irc.cgi", "interface=mozilla&item=style&style=default");
            //HttpGet("http://webchat.xs4all.nl/cgi-bin/ircnet/irc.cgi", "serv=IRCnet&chan=%23" + channel + "&format=default&name=CGI%3AIRC%20User&nick=[sl]Ivan2&interface=mozilla&charset=&R=" + random + "&item=fwindowlist&style=default");
            //HttpGet("http://webchat.xs4all.nl/cgi-bin/ircnet/irc.cgi", "item=fuserlist&interface=mozilla&style=default");
            //HttpGet("http://webchat.xs4all.nl/cgi-bin/ircnet/irc.cgi", "item=blank&style=default");
            //HttpGet("http://webchat.xs4all.nl/cgi-bin/ircnet/irc.cgi", "interface=mozilla&item=style&style=default");

            HttpGet("http://webchat.xs4all.nl/cgi-bin/ircnet/nph-irc.cgi", "style=default&name=CGI%3AIRC%20User&charset=&nick=[sl]Ivan2&serv=IRCnet&format=default&chan=%23"+channel+"&interface=mozilla&R=" + random);
            HttpGet("http://webchat.xs4all.nl/cgi-bin/ircnet/client-perl.cgi", "item=say&cmd=say&say=" + message + "&target=%23" + channel + "&R=" + random + "&xmlhttp=1&");
        }
        void HttpPost(string s, string parameters)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(s);
            request.Proxy = new System.Net.WebProxy("tns2:8082", true);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameters);
            request.ContentLength = bytes.Length;
            System.IO.Stream os = request.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            StreamReader readStream = new StreamReader(receiveStream, encode);

            while ( !readStream.EndOfStream )
                incomingText += (readStream.ReadLine() + "\n");

            readStream.Close();
            response.Close();
        }

        string HttpGet(string URI, string Parameters)
        {
            URI = URI + "?" + Parameters;
            System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
            req.Proxy = new System.Net.WebProxy("tns2:8082", true);
            //req.Proxy = new System.Net.WebProxy(ProxyString, true);
//             req.ContentType = "application/x-www-form-urlencoded";
//             req.Method = "GET";

//             byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
//             req.ContentLength = bytes.Length;
// 
//             System.IO.Stream os = req.GetRequestStream();
//             os.Write(bytes, 0, bytes.Length);
//             os.Close();

            System.Net.WebResponse resp = req.GetResponse();
            if (resp == null) return null;

            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string s = ".";
            while (!sr.EndOfStream && s.Length > 0 && s != "<!-- nothing comment -->")
            {
                s = sr.ReadLine();
                System.Diagnostics.Debug.WriteLine(s);
                incomingText += (s + "\n");
            }

            return "";
            //return sr.ReadToEnd().Trim();
        }
        string GetRandomValue()
        {
            string pattern = "charset=&R=";
            int index = incomingText.IndexOf(pattern);
            if (index == -1)
                return "";
            index += pattern.Length;
            int index2 = incomingText.IndexOf("&", index);
            return incomingText.Substring(index, index2 - index);
        }

        public string incomingText;
    }
}